package com.example.studentmanager.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.studentmanager.R;
import com.example.studentmanager.db.CourseManager;
import com.example.studentmanager.db.entity.Course;

public class AddCourseActivity extends AppCompatActivity {
    private EditText etCourseName;
    private EditText etCourseTeacher;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_course);
        initView();
    }

    private void initView() {
        etCourseName = findViewById(R.id.etCourseName);
        etCourseTeacher = findViewById(R.id.etCourseTeacher);

        findViewById(R.id.btSave).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCourse();
            }
        });
    }

    private void addCourse() {
        String name = etCourseName.getText().toString();
        String teacher = etCourseTeacher.getText().toString();
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(teacher)) {
            Toast.makeText(this, "课程名称和授课老师都不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        Course course = new Course();
        course.setName(name);
        course.setTeacher(teacher);
        if (CourseManager.isExistsCourse(course)) {
            Toast.makeText(this, "课程已经存在", Toast.LENGTH_SHORT).show();
            return;
        }
        if (CourseManager.addCourse(course)) {
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "添加失败", Toast.LENGTH_SHORT).show();
        }
        finish();
    }
}
